package com.evan.review.architecture.components;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.evan.review.R;
import com.evan.review.architecture.components.models.User;
import com.evan.review.databinding.ActivityDataBindingExampleBinding;

/**
 * DataBinding示例Activity
 * 展示DataBinding的基本用法
 */
public class DataBindingExampleActivity extends AppCompatActivity {
    
    private ActivityDataBindingExampleBinding binding;
    private User user;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 使用DataBindingUtil.setContentView替代setContentView
        binding = DataBindingUtil.setContentView(this, R.layout.activity_data_binding_example);
        
        // 设置标题
        setTitle("DataBinding示例");
        
        // 创建用户对象
        user = new User("张三", "zhang.san@example.com", 30);
        
        // 将用户对象设置到绑定中
        binding.setUser(user);
        
        // 设置ClickHandler
        binding.setClickHandler(new MyClickHandler());
        
        // 设置Lifecycle所有者，使LiveData能够正确工作
        binding.setLifecycleOwner(this);
    }
    
    /**
     * 点击处理器类
     */
    public class MyClickHandler {
        
        /**
         * 更新用户信息
         */
        public void onUpdateClick(View view) {
            user.setName("李四");
            user.setEmail("li.si@example.com");
            user.setAge(35);
            
            // 通知绑定刷新 (对于普通POJO对象)
            binding.invalidateAll();
            
            Toast.makeText(DataBindingExampleActivity.this, 
                    "用户信息已更新", Toast.LENGTH_SHORT).show();
        }
        
        /**
         * 重置用户信息
         */
        public void onResetClick(View view) {
            user.setName("张三");
            user.setEmail("zhang.san@example.com");
            user.setAge(30);
            
            // 通知绑定刷新
            binding.invalidateAll();
            
            Toast.makeText(DataBindingExampleActivity.this, 
                    "用户信息已重置", Toast.LENGTH_SHORT).show();
        }
        
        /**
         * 显示用户详情
         */
        public void onShowDetailsClick(View view) {
            String message = String.format("姓名: %s\n邮箱: %s\n年龄: %d", 
                    user.getName(), user.getEmail(), user.getAge());
            
            Toast.makeText(DataBindingExampleActivity.this, 
                    message, Toast.LENGTH_LONG).show();
        }
    }
} 